import type { DepartmentJobResult } from "@/api/siderBar/model";
import { getSettingGetdepartmentjobs } from "@/api/siderBar/setting";
import type { CascaderProps } from "ant-design-vue";
import type { Options } from "vue-request";

export function useFetchDepJobs(requestOptions?: Options<DepartmentJobResult[], []>) {
    const { data: depJobs, refreshAsync, loading } = useRequest(() => getSettingGetdepartmentjobs(), {
        initialData: [],
        ...requestOptions
    })
    const normalDepJobs = computed(() => {
        if (depJobs.value) {
            return depJobs.value.filter(depJob => depJob.departmentId > 0)
        }
        return []
    })
    const depJobOptions = computed<CascaderProps['options']>(() => {
        return normalDepJobs.value.map(depJob => {
            return {
                value: depJob.departmentId,
                label: depJob.departmentName!,
                children: depJob.jobs?.map(job => {
                    return {
                        value: job.jobId,
                        label: job.jobName!
                    }
                })
            }
        })
    });
    return {
        refreshAsync,
        depJobs,
        normalDepJobs,
        depJobOptions,
        loading
    }
}